package cn.iocoder.yudao.module.acm.service.onboardsupplyplan;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.acm.controller.admin.onboardsupplyplan.vo.OnboardSupplyPlanPageReqVO;
import cn.iocoder.yudao.module.acm.controller.admin.onboardsupplyplan.vo.OnboardSupplyPlanRespVO;
import cn.iocoder.yudao.module.acm.controller.admin.onboardsupplyplan.vo.OnboardSupplyPlanSaveReqVO;
import cn.iocoder.yudao.module.acm.controller.admin.onboardsupplyplan.vo.SupplyPlanDetailRespVO;
import cn.iocoder.yudao.module.acm.dal.dataobject.onboardsupplyplan.OnboardSupplyPlanDO;
import cn.iocoder.yudao.module.acm.dal.dataobject.supplyplandetail.SupplyPlanDetailDO;
import cn.iocoder.yudao.module.acm.dal.mysql.onboardsupplyplan.OnboardSupplyPlanMapper;
import cn.iocoder.yudao.module.acm.dal.mysql.supplyplandetail.SupplyPlanDetailMapper;
import cn.iocoder.yudao.module.acm.enums.ACMCabinClassEnum;
import cn.iocoder.yudao.module.pplan.enums.PPlanProduceMarkStatusEnum;
import cn.iocoder.yudao.module.pplan.enums.PPlanProduceStatusEnum;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.acm.enums.ErrorCodeConstants.ONBOARD_SUPPLY_PLAN_NOT_EXISTS;

/**
 * 航机管理-机供品生产计划 Service 实现类
 *
 * @author yisl
 */
@Service
@Validated
public class OnboardSupplyPlanServiceImpl implements OnboardSupplyPlanService {

    @Resource
    private OnboardSupplyPlanMapper onboardSupplyPlanMapper;
    @Resource
    private SupplyPlanDetailMapper supplyPlanDetailMapper;


    @Override
    public Long createOnboardSupplyPlan(OnboardSupplyPlanSaveReqVO createReqVO) {
        // 插入
        OnboardSupplyPlanDO onboardSupplyPlan = BeanUtils.toBean(createReqVO, OnboardSupplyPlanDO.class);
        onboardSupplyPlan.setMarkStatus(Integer.valueOf(PPlanProduceMarkStatusEnum.MARK_STATUS_1.getCode()));
        onboardSupplyPlan.setStatus(Integer.valueOf(PPlanProduceStatusEnum.STATUS_ENUM_1.getCode()));
        onboardSupplyPlan.setPlanCode("P"+System.currentTimeMillis());
        onboardSupplyPlanMapper.insert(onboardSupplyPlan);
        //插入机供品详细
        List<SupplyPlanDetailDO> detailDOS = new ArrayList<>();
        if (CollUtil.isNotEmpty(createReqVO.getFrontDetailList())){
            detailDOS.addAll(BeanUtils.toBean(createReqVO.getFrontDetailList(), SupplyPlanDetailDO.class, clueVO -> {
                clueVO.setSupplyPlanId(onboardSupplyPlan.getId());
                clueVO.setCabainClass(Integer.valueOf(ACMCabinClassEnum.CABIN_CLASS_ENUM_1.getCode()));
            }));
        }
        if (CollUtil.isNotEmpty(createReqVO.getMiddleDetailList())){
            detailDOS.addAll(BeanUtils.toBean(createReqVO.getMiddleDetailList(), SupplyPlanDetailDO.class, clueVO -> {
                clueVO.setSupplyPlanId(onboardSupplyPlan.getId());
                clueVO.setCabainClass(Integer.valueOf(ACMCabinClassEnum.CABIN_CLASS_ENUM_2.getCode()));
            }));
        }
        if (CollUtil.isNotEmpty(createReqVO.getRearDetailList())){
            detailDOS.addAll(BeanUtils.toBean(createReqVO.getRearDetailList(), SupplyPlanDetailDO.class, clueVO -> {
                clueVO.setSupplyPlanId(onboardSupplyPlan.getId());
                clueVO.setCabainClass(Integer.valueOf(ACMCabinClassEnum.CABIN_CLASS_ENUM_3.getCode()));
            }));
        }
        supplyPlanDetailMapper.insertBatch(detailDOS);
        // 返回
        return onboardSupplyPlan.getId();
    }

    @Override
    public void updateOnboardSupplyPlan(OnboardSupplyPlanSaveReqVO updateReqVO) {
        // 校验存在
        validateOnboardSupplyPlanExists(updateReqVO.getId());
        // 更新
        OnboardSupplyPlanDO updateObj = BeanUtils.toBean(updateReqVO, OnboardSupplyPlanDO.class);
        onboardSupplyPlanMapper.updateById(updateObj);

        // 机供品详情更新-先删除-再新增
        supplyPlanDetailMapper.delete(SupplyPlanDetailDO::getSupplyPlanId, updateReqVO.getId());
        //插入机供品详细
        List<SupplyPlanDetailDO> detailDOS = new ArrayList<>();
        if (CollUtil.isNotEmpty(updateReqVO.getFrontDetailList())){
            detailDOS.addAll(BeanUtils.toBean(updateReqVO.getFrontDetailList(), SupplyPlanDetailDO.class, clueVO -> {
                clueVO.setSupplyPlanId(updateReqVO.getId());
                clueVO.setCabainClass(Integer.valueOf(ACMCabinClassEnum.CABIN_CLASS_ENUM_1.getCode()));
            }));
        }
        if (CollUtil.isNotEmpty(updateReqVO.getMiddleDetailList())){
            detailDOS.addAll(BeanUtils.toBean(updateReqVO.getMiddleDetailList(), SupplyPlanDetailDO.class, clueVO -> {
                clueVO.setSupplyPlanId(updateReqVO.getId());
                clueVO.setCabainClass(Integer.valueOf(ACMCabinClassEnum.CABIN_CLASS_ENUM_2.getCode()));
            }));
        }
        if (CollUtil.isNotEmpty(updateReqVO.getRearDetailList())){
            detailDOS.addAll(BeanUtils.toBean(updateReqVO.getRearDetailList(), SupplyPlanDetailDO.class, clueVO -> {
                clueVO.setSupplyPlanId(updateReqVO.getId());
                clueVO.setCabainClass(Integer.valueOf(ACMCabinClassEnum.CABIN_CLASS_ENUM_3.getCode()));
            }));
        }
        supplyPlanDetailMapper.insertBatch(detailDOS);
    }

    @Override
    public void deleteOnboardSupplyPlan(Long id) {
        // 校验存在
        validateOnboardSupplyPlanExists(id);
        // 删除
        onboardSupplyPlanMapper.deleteById(id);
        supplyPlanDetailMapper.delete(SupplyPlanDetailDO::getSupplyPlanId, id);
    }

    @Override
    public void deleteOnboardSupplyPlanListByIds(List<Long> ids) {
        // 校验存在
        validateOnboardSupplyPlanExists(ids);
        // 删除
        onboardSupplyPlanMapper.deleteByIds(ids);
    }

    private void validateOnboardSupplyPlanExists(List<Long> ids) {
        List<OnboardSupplyPlanDO> list = onboardSupplyPlanMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(ONBOARD_SUPPLY_PLAN_NOT_EXISTS);
        }
    }

    private void validateOnboardSupplyPlanExists(Long id) {
        if (onboardSupplyPlanMapper.selectById(id) == null) {
            throw exception(ONBOARD_SUPPLY_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public OnboardSupplyPlanRespVO getOnboardSupplyPlan(Long id) {
        OnboardSupplyPlanDO supplyPlanDO = onboardSupplyPlanMapper.selectById(id);
        OnboardSupplyPlanRespVO onboardSupplyPlanRespVO = BeanUtils.toBean(supplyPlanDO, OnboardSupplyPlanRespVO.class);

        List<SupplyPlanDetailDO> detailDOS = supplyPlanDetailMapper.selectList(SupplyPlanDetailDO::getSupplyPlanId, id);
        if (CollUtil.isNotEmpty(detailDOS)) {
            List<SupplyPlanDetailRespVO> respVOS = BeanUtils.toBean(detailDOS, SupplyPlanDetailRespVO.class);
            Map<String, List<SupplyPlanDetailRespVO>> collect = respVOS.stream().collect(Collectors.groupingBy(SupplyPlanDetailRespVO::getCabainClass));
            onboardSupplyPlanRespVO.setFrontDetailList(collect.get(ACMCabinClassEnum.CABIN_CLASS_ENUM_1.getCode()));
            onboardSupplyPlanRespVO.setMiddleDetailList(collect.get(ACMCabinClassEnum.CABIN_CLASS_ENUM_2.getCode()));
            onboardSupplyPlanRespVO.setRearDetailList(collect.get(ACMCabinClassEnum.CABIN_CLASS_ENUM_3.getCode()));
        }
        return onboardSupplyPlanRespVO;
    }

    @Override
    public PageResult<OnboardSupplyPlanDO> getOnboardSupplyPlanPage(OnboardSupplyPlanPageReqVO pageReqVO) {
        return onboardSupplyPlanMapper.selectPage(pageReqVO);
    }
}